/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using EXtremecode.Utils;
using System.Data;
using System.Data.SqlClient;
using EXtremecode.DataAccessLayer; 
using System.Collections;
using System.Text;
using EXtremecode.Common;
using System.Data.Common;
namespace EXtremecode.DataAccessLayer
{
	/// <summary>
	/// Summary description for SQLServerDataAccessor.
	/// </summary>
	public class SQLServer05DataAccessor:SQLServerDataAccessor
	{
		
        public SQLServer05DataAccessor(string sAccessorName,string sConnectionURL):this(sAccessorName,sConnectionURL,true)
		{
			
		}

        public SQLServer05DataAccessor(string sAccessorName, string sConnectionURL, bool validationRequired)
            : base(sAccessorName, sConnectionURL,validationRequired)
        {

        }

        internal override string GeneratePagingQuery(string selectQuery, string sortedBy, bool isSortingTypeAscending, int pageNumber, int pageSize, long? totalRecords)
        {
            if (sortedBy.Trim() == string.Empty)
            {
                throw new Exception("[UC] Sorted Column should be defined");
            }

            string sortingType = (isSortingTypeAscending) ? " ASC " : " DESC ";
            long bottomRowNum = pageNumber * pageSize;
            long topRowNum = bottomRowNum - pageSize + 1;


            //enclose original query;
            string firstQuery = string.Format(@"Select * from 
                (
                    select query1.*,row_number() over(order by {0}) as row_num  from
                    (
                        {1}
                    ) query1
                )query1 where row_num >= {2} and row_num <= {3}",
                sortedBy.Replace(",", sortingType + ",") + sortingType,
                selectQuery, topRowNum, bottomRowNum);


            return firstQuery;
        }
    }
}
